<?php
/**
 * 权限管理a
 * Created by gii.
 * User: Gii
 * Date: 2022/04/19
 * Time: 19:15
 */

namespace App\Models\Models\System;

use App\Models\Traits\Common;
use Illuminate\Database\Eloquent\Model;

class SystemAccountAuth extends Model
{
	use Common;
    protected $primaryKey = 'account_id';

    public static function addByAccount($accountId) {
        $data = [
            'account_id' => $accountId,
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        self::addCommon($data);
        return $accountId;
    }

    public static function check($accountId, $menuId) {
        $menuIdList = self::getMenuIdByAccountId($accountId);
        if(!in_array($menuId, $menuIdList)) {
            return false;
        }
        return true;
    }

    public static function getOneByAccount($accountId){
        return self::getLineCommon(['account_id'=>$accountId]);
    }

    // 修改菜单ID
    public static function changeMenuId($menuIds, $accountId) {
        $where = [
            'account_id' => $accountId,
        ];
        $menuIds[] = 1; // 首页
        $data = [
            'menu_id' => json_encode($menuIds, JSON_UNESCAPED_UNICODE),
        ];
        if(self::existCommon($where, 'account_id')) {
            return self::updateCommon($data, $where);
        }

        $data = $data + $where;
        return self::addCommon($data);
    }

    public static function getMenuIdByAccountId($accountId) {
        $where = [
            'account_id' => $accountId,
        ];
        $menuIds = self::getValueCommon($where, 'menu_id');
        return json_decode($menuIds, true);
    }

    public function getMenuId() {
        return json_decode($this->menu_id, true) ?? [];
    }
}
